#!/bin/bash

###############################################
#
#   GIN INSTALL SCALA ENV
#	将 sh,scala 上传至linux自己存放软件的目录(例:softwares)
#	再配置 scala_install_path 即可
#   注意: 确认 ls -l|awk '{print$9} 获取的是文件名
#	
#	Upload SH and SCALA to the directory where Linux stores the software(for example, softwares)
#	Then configure SCALA_Install_Path
#	Note: make sure ` ls -l|awk'{print$9}' ` gets the filename
#
###############################################
scala_install_path="/usr/local"
# scala_package_name 与 scala_unzip_dir 为全局变量初始值,后续会被命令替换,可不配
# scala_package_name and scala_unzip_dir are the initial values of global variables.
# They will be replaced by commands later. They need not configured
scala_package_name="scala-2.11.12.tgz"
scala_unzip_dir="scala-2.11.12"
###############################################
#
#	check_package
#	install_scala
#	test_scala
#
###############################################

function check_package()
{
	### check scala_install_path exists
	if [ ! -d "${scala_install_path}" ];
	then
		echo "${scala_install_path} not exit, mkdir"
		mkdir -p "${scala_install_path}"
	else
		echo "${scala_install_path} is exit"
	fi
	### get scala.tar package name from current file
    scala_package_name=`ls | grep scala | grep .tgz`
	
	### check package
    if [ ! -f "${scala_package_name}" ];
    then
		echo "you need scala package!"
        exit
    fi    
 
    ### check unzip tmp dir
	scala_unzip_dir=`ls -l | grep '^d' |grep scala |awk '{print$9}'`
	if [ -d "${scala_unzip_dir}" ];
    then
		echo "${scala_unzip_dir} is exit, rm unzip path"
        rm -rf "${scala_unzip_dir}"
    fi
	
	
 
}
################################################
#
#   install scala
#
#################################################
function install_scala()
{
	### unzip
	tar -zxf "${scala_package_name}"
	### get file name
	scala_unzip_dir=`ls -l | grep '^d' |grep scala |awk '{print$9}'`
	### move file to install path
	echo "scala_unzip_dir=${scala_unzip_dir}, scala_install_path=${scala_install_path}"
	if [ -d "${scala_install_path}/${scala_unzip_dir}" ];
    then
		echo "${scala_install_path}/${scala_unzip_dir} is exit, rm install path"
        rm -rf "${scala_install_path}/${scala_unzip_dir}"
    fi
    mv "${scala_unzip_dir}" "${scala_install_path}"/
    
	### apend settings
    echo "# scala environment begin" >> /etc/profile
    echo "SCALA_HOME=${scala_install_path}/${scala_unzip_dir}" >>/etc/profile
    echo "PATH=\$PATH:\${SCALA_HOME}/bin" >>/etc/profile
    echo "export PATH SCALA_HOME " >>/etc/profile
    echo "# scala environment end" >>/etc/profile
	### source
	source /etc/profile
	### append user settings like /home/${USER}/.bash_profile
}

function test_scala()
{
# 5. Test scala evironment
  if [ ! -d $SCALA_HOME ];
  then 
    echo "-->Failed to install SCALA"
  else  
    echo "-->SCALA has been successed installed."
    echo "scala -version"
    scala -version
    echo "ls \$SCALA_HOME"
    ls $SCALA_HOME
  fi
}  
 
function main()
{
	### Execute as needed
    check_package
    install_scala
	  test_scala
}
 
# Execute main method 
main
# END



